package com.whitehot.klib.crash;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import com.whitehot.klib.crash.bean.Column;
import com.whitehot.klib.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yutf on 2017/8/30 0030.
 */

public class MyDbHelper extends SQLiteOpenHelper
{

    private static final String DB_NAME = "crash_log";
    private Context mContext;

    private final String SQL = "create table " + Column.TABLE_NAME
            + " (" + Column._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + Column.RECORD_CRASH_TIME + " INTEGER,"
            + Column.RECORD_IS_UPLOADED + " INTEGER,"
            + Column.RECORD_FILE_PATH + " TEXT);";

    public MyDbHelper(Context context)
    {
        super(context, DB_NAME, null, 1);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(SQL);
        LogUtil.e("创建异常记录表");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    public int getCount()
    {
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.query(Column.TABLE_NAME, new String[]{Column._ID}, null,null,null,null,null);
        int size = c.getCount();
        c.close();
        return size;
    }

    public long insert(CrashBean item)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(Column.RECORD_CRASH_TIME, System.currentTimeMillis());
        cv.put(Column.RECORD_IS_UPLOADED, item.isUploaded);
        cv.put(Column.RECORD_FILE_PATH, item.logFilePath);
        return db.insert(Column.TABLE_NAME, null, cv);
    }

    public void update(CrashBean item)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(Column.RECORD_IS_UPLOADED, item.isUploaded);
        db.update(Column.TABLE_NAME, cv, Column._ID + "=?", new String[]{item.id.toString()});
    }

    public List<CrashBean> list()
    {
        SQLiteDatabase db = getReadableDatabase();
        List<CrashBean> all = new ArrayList<>();
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(Column.TABLE_NAME);
        String sql = builder.buildQuery(null, Column.RECORD_IS_UPLOADED + "=?" , null, null,null,null);
//        Cursor c = db.query(Column.TABLE_NAME, null, Column.RECORD_IS_UPLOADED + "=?",new String[]{"0"},null,null, null, null);
        Cursor c = db.rawQuery(sql, new String[]{"0"});
        LogUtil.e("_crash sql : " + sql);
        if(c.moveToFirst())
        {
            do {
                int idix = c.getColumnIndex(Column._ID);
                Long id = c.getLong(idix);
                int isUploadIx = c.getColumnIndex(Column.RECORD_IS_UPLOADED);
                int isUpload = c.getInt(isUploadIx);
                int pathIx = c.getColumnIndex(Column.RECORD_FILE_PATH);
                String path = c.getString(pathIx);
                int timeIx = c.getColumnIndex(Column.RECORD_CRASH_TIME);
                long time = c.getLong(timeIx);

                CrashBean r = new CrashBean();
                r.id = id;
                r.isUploaded = isUpload;
                r.crash_time = time;
                r.logFilePath = path;
                all.add(r);
            }while (c.moveToNext());
        }
        c.close();
        return all;
    }

    public void remove(CrashBean item)
    {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(Column.TABLE_NAME, Column._ID + "=?", new String[]{item.id.toString()});
    }
}
